package com.healthy.sport.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.healthy.sport.demo.datasource.DynamicRoutingDataSource;
import com.healthy.sport.demo.dto.DatasourceSQL;
import com.healthy.sport.demo.utils.ConUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/11/10
 */
@Slf4j
@Configuration
public class DynamicDataSourceConfig {





    @Primary
    @Bean
    public DataSource dynamicDataSource() {

        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
//        dataSource.setDefaultTargetDataSource();设置默认的数据库

        List<DatasourceSQL> list = ConUtil.get();

        Map<Object, Object> dataSourceMap = new HashMap<>(list.size());
        for (DatasourceSQL datasourceSQL : list) {
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setDriverClassName(datasourceSQL.getDiverClassName());
            druidDataSource.setUrl(datasourceSQL.getUrl());
            druidDataSource.setUsername(datasourceSQL.getUsername());
            druidDataSource.setPassword(datasourceSQL.getPassword());
            dataSourceMap.put(datasourceSQL.getSchool_id(), druidDataSource);
        }

        dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
        return dynamicRoutingDataSource;

    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
       /* SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource());
        SqlSessionFactory sqlSessionFactory = null;
        //此处设置为了解决找不到mapper文件的问题
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));
        sqlSessionFactory = sqlSessionFactoryBean.getObject();*/

//      要切换成mybatis的
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dynamicDataSource());
        mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));

        return mybatisSqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory());
    }

    /**
     * 事务管理
     *
     * @return 事务管理实例
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }

}
